#!/bin/sh
# =====================================================
# Kwrt 卸载 cpolar 干净脚本 (ARMv8 aarch64)
# =====================================================

echo "[1/6] 停止 cpolar 服务..."
BINARY="/usr/bin/cpolar"
if [ -x "$BINARY" ]; then
    $BINARY stop-all || true
fi

# 如存在 init.d 服务，尝试停止并禁用
if [ -x "/etc/init.d/cpolar" ]; then
    /etc/init.d/cpolar stop || true
    /etc/init.d/cpolar disable || true
fi

echo "[2/6] 终止残留进程..."
killall cpolar 2>/dev/null || true

echo "[3/6] 删除可执行文件..."
if [ -f "$BINARY" ]; then
    rm -f "$BINARY"
fi

echo "[4/6] 删除配置与日志目录..."
CPOLAR_DIR="/usr/cpolar"
if [ -d "$CPOLAR_DIR" ]; then
    rm -rf "$CPOLAR_DIR"
fi

echo "[5/6] 清理下载与临时文件..."
rm -f "$HOME"/cpolar-stable-linux-*.zip 2>/dev/null || true
rm -f /tmp/cpolar 2>/dev/null || true

# 如存在 init.d 脚本和 rc.d 软链，清理之
if [ -f "/etc/init.d/cpolar" ]; then
    rm -f /etc/init.d/cpolar || true
fi
rm -f /etc/rc.d/*cpolar* 2>/dev/null || true

echo "[6/6] 验证卸载结果..."
if command -v cpolar >/dev/null 2>&1; then
    echo "仍检测到 cpolar 可执行，请检查 PATH 或手动清理残留。"
else
    echo "cpolar 已卸载干净。"
fi

exit 0


